/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package unilearn.test.bo;

import java.util.List;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import unilearn.bo.AssignmentManagement;
import unilearn.bo.PersonManagement;
import unilearn.bo.StudentGroupManagement;
import unilearn.bo.SubjectManagement;
import unilearn.dao.hibernate.AssignmentDAOImpl;
import unilearn.dao.hibernate.GroupMemberDAOImpl;
import unilearn.dao.hibernate.PersonDAOImpl;
import unilearn.dao.hibernate.PersonRoleDAOImpl;
import unilearn.dao.hibernate.RoleDAOImpl;
import unilearn.dao.hibernate.StudentGroupDAOImpl;
import unilearn.dao.hibernate.SubjectDAOImpl;
import unilearn.dao.hibernate.SubjectMarkerDAOImpl;
import unilearn.dao.hibernate.SubjectStudentDAOImpl;
import unilearn.vo.Assignment;
import unilearn.vo.GroupMember;
import unilearn.vo.Person;
import unilearn.vo.StudentGroup;
import unilearn.vo.Subject;

/**
 *
 * @author Ming
 */
public class StudentGroupUnit {

    private StudentGroupManagement groupMgt;
    private SubjectManagement subjectMgt;
    private AssignmentManagement assignMgt;
    private PersonManagement personMgt;

    public StudentGroupUnit() {
        groupMgt = new StudentGroupManagement();
        groupMgt.setGroupMemberDAO(new GroupMemberDAOImpl());
        groupMgt.setPersonRoleDAO(new PersonRoleDAOImpl());
        groupMgt.setRoleDAO(new RoleDAOImpl());
        groupMgt.setPersonDAO(new PersonDAOImpl());
        groupMgt.setStudentGroupDAO(new StudentGroupDAOImpl());

        subjectMgt = new SubjectManagement();
        subjectMgt.setPersonDAO(new PersonDAOImpl());
//        subjectMgt.setPersonRoleDAO(new PersonRoleDAOImpl());
//        subjectMgt.setRoleDAO(new RoleDAOImpl());
        subjectMgt.setSubjectDAO(new SubjectDAOImpl());
        subjectMgt.setSubjectMarkerDAO(new SubjectMarkerDAOImpl());
        subjectMgt.setSubjectStudentDAO(new SubjectStudentDAOImpl());

        assignMgt = new AssignmentManagement();
        assignMgt.setAssignmentDAO(new AssignmentDAOImpl());

        personMgt = new PersonManagement();
        personMgt.setPersonDAO(new PersonDAOImpl());
        personMgt.setPersonRoleDAO(new PersonRoleDAOImpl());
        personMgt.setRoleDAO(new RoleDAOImpl());
    }

    @BeforeClass
    public static void setUpClass() throws Exception {
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }

    @Before
    public void setUp() {
    }

    @After
    public void tearDown() {
    }

    @Test
    public void test() throws Exception {
//        this.testGetAllStudentGroup();
//        this.testCreateStudentGroup();
//        this.testUpdateStudentGroup();
        this.testRemoveStudentGroup();
    }

    private void testGetAllStudentGroup() throws Exception {
        try {
            System.out.println(" ========================= [ testGetAllStudentGroup ] ========================= ");
            List<Person> lecturers = personMgt.getAllLecturer();
            if ((lecturers == null) || (lecturers.isEmpty())) {
                throw new Exception("no lecturer.");
            }

            List<Subject> subjects = subjectMgt.getAllSubjects();
            if ((subjects == null) || (subjects.isEmpty())) {
                throw new Exception("no subject.");
            }

            for (Subject subject : subjects) {
                System.out.println("[Subject]" + subject.getSubjectCode() + " : " + subject.getName());
                List<Assignment> assigns = assignMgt.getAssignments(subject);
                if ((assigns == null) || (assigns.isEmpty())) {
                    System.out.println("This subject has not assignment.");
                } else {
                    for (Assignment assign : assigns) {
                        if (assign.getType().equals("G")) {
                            System.out.println("[Assignment]" + assign.getId() + " : " + assign.getName());
                            List<StudentGroup> groups = groupMgt.getStudentGroups(assign, lecturers.get(0));
                            if ((groups == null) || (groups.isEmpty())) {
                                System.out.println("This assignment has not student group.");
                            } else {
                                for (StudentGroup group : groups) {
                                    System.out.println(" + " + group.toString());
                                    List<Person> members = groupMgt.getGroupMembers(group);
                                    if ((members == null) || (members.isEmpty())) {
                                        System.out.println("This student group has not group member.");
                                    } else {
                                        for (Person member : members) {
                                            System.out.println(" - " + member.toString());
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            System.out.println(" XXXXXXXXXXXXXXXXXXXXXXXXX [ testGetAllStudentGroup ] XXXXXXXXXXXXXXXXXXXXXXXXX ");
            System.out.println(e.toString());
            System.out.println(" XXXXXXXXXXXXXXXXXXXXXXXXX [ testGetAllStudentGroup ] XXXXXXXXXXXXXXXXXXXXXXXXX ");
        }
    }

    private void testCreateStudentGroup() throws Exception {
        try {
            System.out.println(" ========================= [ testCreateStudentGroup ] ========================= ");
            List<Subject> subjects = subjectMgt.getAllSubjects();
            if ((subjects == null) || (subjects.isEmpty())) {
                throw new Exception("no subject.");
            }

            for (Subject subject : subjects) {
                System.out.println("[Subject]" + subject.getSubjectCode() + " : " + subject.getName());
                List<Assignment> assigns = assignMgt.getAssignments(subject);
                if ((assigns == null) || (assigns.isEmpty())) {
                    System.out.println("This subject has not assignment.");
                } else {
                    List<Person> students = subjectMgt.getSubjectStudents(subject);
                    if ((students == null) && (students.isEmpty())) {
                        System.out.println("This subject has not studnet(s).");
                    } else {
                        for (Assignment assign : assigns) {
                            if (assign.getType().equals("G")) {
                                System.out.println("[Assignment]" + assign.getId() + " : " + assign.getName());
                                StudentGroup group = groupMgt.createStudentGroup(assign, students);
                                System.out.println("[created]group: " + (group == null ? "[null]" : group.toString()));
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            System.out.println(" XXXXXXXXXXXXXXXXXXXXXXXXX [ testCreateStudentGroup ] XXXXXXXXXXXXXXXXXXXXXXXXX ");
            System.out.println(e.toString());
            System.out.println(" XXXXXXXXXXXXXXXXXXXXXXXXX [ testCreateStudentGroup ] XXXXXXXXXXXXXXXXXXXXXXXXX ");
        }
    }

    private void testUpdateStudentGroup() throws Exception {
        try {
            System.out.println(" ========================= [ testUpdateStudentGroup ] ========================= ");
            List<Person> lecturers = personMgt.getAllLecturer();
            if ((lecturers == null) || (lecturers.isEmpty())) {
                throw new Exception("no lecturer.");
            }

            List<Subject> subjects = subjectMgt.getAllSubjects();
            if ((subjects == null) || (subjects.isEmpty())) {
                throw new Exception("no subject.");
            }

            for (Subject subject : subjects) {
                System.out.println("[Subject]" + subject.getSubjectCode() + " : " + subject.getName());
                List<Assignment> assigns = assignMgt.getAssignments(subject);
                if ((assigns == null) || (assigns.isEmpty())) {
                    System.out.println("This subject has not assignment.");
                } else {
                    List<Person> students = subjectMgt.getSubjectStudents(subject);
                    if ((students == null) && (students.isEmpty())) {
                        System.out.println("This subject has not studnet(s).");
                    } else {
                        for (Assignment assign : assigns) {
                            if (assign.getType().equals("G")) {
                                System.out.println("[Assignment]" + assign.getId() + " : " + assign.getName());

                                List<StudentGroup> groups = groupMgt.getStudentGroups(assign, lecturers.get(0));
                                if ((groups == null) && (groups.isEmpty())) {
                                    System.out.println("This assignment has student group.");
                                } else {
                                    for (StudentGroup group : groups) {
                                        List<Person> members = groupMgt.getGroupMembers(group);
                                        if ((groups == null) && (groups.isEmpty())) {
                                            System.out.println("This student group has member.");
                                        } else {

                                            if ((members == null) || (members.isEmpty())) {
                                                System.out.println("This student group(ID: " + group.getId() + ") has member.");
                                            } else {
                                                System.out.println("Update student group");
                                                members.remove(0);
                                                members = groupMgt.updateGroupMembers(group, members);

                                                if ((members == null) || (members.isEmpty())) {
                                                    System.out.println("This student group(ID: " + group.getId() + ") has member.");
                                                } else {
                                                    System.out.println("[Group]" + group.toString());
                                                    for (Person member : members) {
                                                        System.out.println("[Member]" + member.toString());
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            System.out.println(" XXXXXXXXXXXXXXXXXXXXXXXXX [ testUpdateStudentGroup ] XXXXXXXXXXXXXXXXXXXXXXXXX ");
            System.out.println(e.toString());
            System.out.println(" XXXXXXXXXXXXXXXXXXXXXXXXX [ testUpdateStudentGroup ] XXXXXXXXXXXXXXXXXXXXXXXXX ");
        }
    }

    private void testRemoveStudentGroup() throws Exception {
        List<Person> lecturers = personMgt.getAllLecturer();
        if ((lecturers == null) || (lecturers.isEmpty())) {
            throw new Exception("no lecturer.");
        }

        List<Subject> subjects = subjectMgt.getAllSubjects();
        if ((subjects == null) || (subjects.isEmpty())) {
            throw new Exception("no subject.");
        }

        for (Subject subject : subjects) {
            System.out.println("[Subject]" + subject.getSubjectCode() + " : " + subject.getName());
            List<Assignment> assigns = assignMgt.getAssignments(subject);
            if ((assigns == null) || (assigns.isEmpty())) {
                System.out.println("This subject has not assignment.");
            } else {
                List<Person> students = subjectMgt.getSubjectStudents(subject);
                if ((students == null) && (students.isEmpty())) {
                    System.out.println("This subject has not studnet(s).");
                } else {
                    for (Assignment assign : assigns) {
                        if (assign.getType().equals("G")) {
                            System.out.println("[Assignment]" + assign.getId() + " : " + assign.getName());

                            List<StudentGroup> groups = groupMgt.getStudentGroups(assign, lecturers.get(0));
                            if ((groups == null) && (groups.isEmpty())) {
                                System.out.println("This assignment has student group.");
                            } else {
                                for (StudentGroup group : groups) {
                                    System.out.println("[Group]removed(ID: " + group.getId() + ").");
                                    groupMgt.removeStudentGroup(group);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    private void testCase() throws Exception {
        try {
            System.out.println(" ========================= [ testGetAllStudentGroup ] ========================= ");
        } catch (Exception e) {
            System.out.println(" XXXXXXXXXXXXXXXXXXXXXXXXX [ testGetAllStudentGroup ] XXXXXXXXXXXXXXXXXXXXXXXXX ");
            System.out.println(e.toString());
            System.out.println(" XXXXXXXXXXXXXXXXXXXXXXXXX [ testGetAllStudentGroup ] XXXXXXXXXXXXXXXXXXXXXXXXX ");
        }
    }
}
